@page "/playground"
@using System.Diagnostics
@using PinguApps.Blazor.QRCode
@using RJCP.IO.Ports
@using System.Net;
@using System.Net.Sockets;
@using Photino.Blazor
@using System.Text;
@implements IDisposable


@inject IDialogService DialogService
@inject PhotinoBlazorApp Photino
@inject ISnackbar Snackbar


@if (_devices.Count > 0)
{
	<MudList T="string">
		@foreach (var d in _devices)
		{
			<MudListItem T="string" Text="@d.ToString()" @onclick="@(()=>OpenTelnet(d.Ip))"  />
		}

	</MudList>
}


<MudStack Class="mt-4">
	<MudStack Row="true" Spacing="4">
		<MudSelect T="string" @bind-value="_comPort" Disabled="@_isOpen">
			@foreach (var p in _comPorts)
			{
				<MudSelectItem T="string" Value="@p">@p</MudSelectItem>
			}
		</MudSelect>
		<MudButton Variant="Variant.Filled" DropShadow="false" Color="Color.Primary" Size="Size.Small"
		           @onclick="OpenCom" Disabled="@(_comPort is "")">@(_isOpen ? "Close" : "Open")</MudButton>
	</MudStack>

	<MudTextField Label="Text Received" T="string" Lines="10" Variant="Variant.Outlined" Text="@_comPortText"></MudTextField>
</MudStack>

<MudButton OnClick="@LoadImageFromDisk" Variant="Variant.Filled" ><MudIcon Icon="@Icons.Material.Filled.Favorite" Title="Favorite" /> Load Img</MudButton>
<MudList T="string" Class="mt-2">

	<MudListItem>
		<MudStack Class="d-flex flex-row" Row AlignItems="AlignItems.Center">
			<MudStack Class="flex-grow-1" Spacing="0">
				<MudText Typo="Typo.body1">192.168.0.123</MudText>
				<MudText Typo="Typo.caption">DV300</MudText>
			</MudStack>
			<MudStack Row>
				<MudIconButton Icon="@Icons.Material.Filled.OpenInBrowser" />
				<MudIconButton Icon="@Icons.Material.Filled.Terminal" />
			</MudStack>
		</MudStack>
	</MudListItem>

</MudList>

@code {

	SerialPortStream? _sp;


	string _comPort = "COM1";
	string _comPortText = "";
	bool _isOpen = false;
	IEnumerable<string> _comPorts = SerialPortStream.GetPortNames();


	private IList<Device> _devices = [];

	string base64Image = "";


	private void OpenTelnet(string ip)
	{
		var info = new ProcessStartInfo()
		{
			FileName = "telnet.exe",
			Arguments = ip,
			UseShellExecute = true
		};
		Process.Start(info);
	}

	private void OpenCom()
	{
		if (_isOpen)
		{
			_sp?.Close();
			_sp = null;
		}
		else
		{
			_sp = new SerialPortStream(_comPort, 9600);

			_sp.DataReceived += (s, e) =>
			{
				var txt = _sp.ReadExisting();
				this.InvokeAsync(() =>
				{
					_comPortText = $"{DateTime.Now}: {txt}{Environment.NewLine}" + _comPortText;
					StateHasChanged();
				});
			};
			_sp.Open();
		}

		_isOpen = !_isOpen;
	}



	public void Dispose()
	{
		_sp?.Close();
		_sp = null;
	}

	private async Task LoadImageFromDisk()
	{
		var  result = await Photino.MainWindow.ShowOpenFileAsync(filters: [("Image", ["png", "jpg", "jpeg", "gif", "bmp", "tiff"])]);
		if (result.Length == 0) return;
		var path = result[0];
		Snackbar.Add(Path.GetFileNameWithoutExtension(path));
		var base64 = await File.ReadAllBytesAsync(path);
		base64Image = $"data:image/jpg;base64,{Convert.ToBase64String(base64)}";
	}



}
